<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_page_w_grid_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/grids/_ci/ci_nom_select/ci_nom_select_grid_ctrl.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/nomenclature/tcms_nomenclature_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_mod_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_attr_types/tcms_cia_nomenclature_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_composite_dbc.class.php');


class CI_Nom_Select_WP extends TCMS_Admin_Page_W_Grid_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_ci/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('nom');
		$this->add_transit_var('ret');
		$this->add_transit_var('ci');
		$this->add_transit_var('ciat');
		$this->add_transit_var('vpage');
	}


	protected function init_events() {
		parent::init_events();

		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('select', Tangra_Parameter_Method::GET, 'select'), 'select_event');
		$this->wed->add_wed_event_action_pair(new Web_Event_Simple_Int('deselect', Tangra_Parameter_Method::GET, 'deselect'), 'deselect_event');
	}


	protected function select_event($item_id) {
		$nom_hid = $this->get_transit_var('nom');
		$ci_id = $this->get_transit_var('ci');
		$ciat_id = $this->get_transit_var('ciat');

		$dbc = $this->get_dbc();

		$nomenclature = new TCMS_Nomenclature_DBC();
		if (!$nomenclature->load_by_hid($dbc, $nom_hid)) {
			throw new Tangra_Module_Exception('Cannot find nomenclature with hid = '.$nom_hid);
		}

		$ci = new TCMS_CI_DBC();
		if (!$ci->load_by_id($dbc, $ci_id)) {
			throw new Tangra_Module_Exception('Cannot find ci with id = '.$ci_id);
		}


		$cia_nom = new TCMS_CIA_Nomenclature_DBC();
		$cia_nom->set_ci($ci_id);
		$cia_nom->set_ci_type_attr($ciat_id);
		$cia_nom->set_value($item_id);
		try {
			$dbc->start_trans();
			$cia_nom->save($dbc);
			$this->update_ci_last_mod($ci_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$ret_page = $this->get_transit_var('ret');

		return $this->default_event();
	}


	protected function deselect_event($cia_nom_id) {
		$dbc = $this->get_dbc();
		try {
			$dbc->start_trans();
			TCMS_CIA_Nomenclature_DBC::delete($dbc, $cia_nom_id);
			$this->update_ci_last_mod($ci_id);
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		return $this->default_event();
	}


	protected function create_grid_ctrl() {
		$tvm = $this->get_tvm();

		$nom_hid = $this->get_transit_var('nom');
		$nomenclature = new TCMS_Nomenclature_DBC();

		$dbc = $this->get_dbc();
		if (!$nomenclature->load_by_hid($dbc, $nom_hid)) {
			throw new Tangra_Module_Exception('Cannot find nomenclature with hid = '.$nom_hid);
		}
		$this->export('nomenclature_name', $nomenclature->get_name());

		$ci_id = $this->get_transit_var('ci');
		$this->export('ci_id', $ci_id);
		$ci = new TCMS_CI_Composite_DBC($this->_config->get_hidden_dir().'inc/');
		if (!$ci->load_by_id($dbc, $ci_id)) {
			throw new Tangra_Module_Exception('Cannot load attachments for this ci item because ci item does not exists. ID = '.$ci_id);
		}
		$this->export('ci_title', $ci->get_as_short_plain_text());

		$ciat_id = $this->get_transit_var('ciat');

		$ret = new CI_Nom_Select_Grid_Ctrl('ci_nom_select', $tvm, Tangra_Parameter_Method::GET, $nomenclature->get_id(), $ci_id, $ciat_id);

		$ret_page = $this->get_transit_var('ret');
		$this->export('ret_page', $ret_page);


		if (!$tvm->is_page_var_registered('ci_nom_select', 'text')) {
			$tvm->register_page_var('ci_nom_select', 'text');

			$tmp2 = 0;
			$tvm->register_page_var('ci_nom_select', 'show');
			$tvm->set_page_var('ci_nom_select', 'show', $tmp2);
		}

		$this->process_filter($tvm);

		return $ret;
	}


	private function update_ci_last_mod($ci_id) {
		$dbc = $this->get_dbc();
		$current_user = $this->get_user();
		$ci_mod = new TCMS_CI_Mod_DBC();
		$ci_mod->set_ci($ci_id);
		$ci_mod->set_modified_by($current_user->get_id());
		$ci_mod->save($dbc);
	}


	private function process_filter($vm) {
		$context = $this->get__context();
		if ($context->exists_in_get('text')) {
			$text = $context->get_from_get('text');
			$vm->set_page_var('ci_nom_select', 'text', $text);
		}

		if ($context->exists_in_get('show')) {
			$show = $context->get_from_get('show');
			$vm->set_page_var('ci_nom_select', 'show', $show);
		}

		$text = $vm->get_page_var('ci_nom_select', 'text');
		$show = $vm->get_page_var('ci_nom_select', 'show');

		$this->export('text', $text);
		$this->export('show', $show);
	}
}

